﻿// JScript File

  // тип вопроса
    var IIT_CHOICE = 2;
    var IIT_CHOICEMULTIPLE = 3;
    var IIT_ORDER = 4;  
    var IIT_ASSOCIATE = 5;
    var IIT_EXTENDEDTEXT = 6;
    var IIT_TEXTENTRY = 7;

function Item(xml)
{    
    this.xml = xml;
    this.id = this.xml.attributes.getNamedItem("identifier").text;
    this.iit = null;                //ItemInteractionType
    this.interaction = null;
    this.rendering = null;            
        
    var body = this.xml.getElementsByTagName("itemBody")[0];    
    if (body.getElementsByTagName("choiceInteraction").length > 0)
    {
        var maxChoices = body.getElementsByTagName("choiceInteraction")[0].attributes.getNamedItem("maxChoices");
        if (!isMozilla ? (maxChoices.text == "0") : (maxChoices.textContent == "0"))
            this.iit = IIT_CHOICEMULTIPLE;
        else
            this.iit = IIT_CHOICE;
    }
    else
    {   
        if (body.getElementsByTagName("orderInteraction").length > 0)
            this.iit = IIT_ORDER;
        else
        {
            if (body.getElementsByTagName("associateInteraction").length > 0)
                this.iit = IIT_ASSOCIATE;
            else
            {
                if (body.getElementsByTagName("extendedTextInteraction").length > 0)
                    this.iit = IIT_EXTENDEDTEXT;                        
                else
                {
                    if (body.getElementsByTagName("textEntryInteraction").length > 0)
                        this.iit = IIT_TEXTENTRY;                        
                }    
            }       
        }       
    }
    
    switch (this.iit)
    {
        case IIT_CHOICE:
        case IIT_CHOICEMULTIPLE:                
            this.interaction = new ChoiceInteraction(this);                    
            this.rendering = new ChoiceRendering(this);            
            break;
        case IIT_ORDER:
            this.interaction = new OrderInteraction(this);                    
            this.rendering = new OrderRendering(this);            
            break;
        case IIT_ASSOCIATE:        
            this.interaction = new AssociateInteraction(this);                    
            this.rendering = new AssociateRendering(this);                        
            break;
        case IIT_EXTENDEDTEXT:
            this.interaction = new ExtendedTextInteraction(this);                    
            this.rendering = new ExtendedTextRendering(this);            
            break;
        case IIT_TEXTENTRY:        
            this.interaction = new TextEntryInteraction(this);                                
            this.rendering = new TextEntryRendering(this);            
            break;
    }
    this.Interact = function(event)
    {
        var answered = this.xml.attributes.getNamedItem("answered");
        
        if (!isMozilla)
            answered.text = "true";
        else
            answered.textContent = "true";
            
        this.interaction.Interact(event);
    }
    this.Render = function()
    {
        this.SetCurrent();
        return this.rendering.Render();
    }
    this.SetItemCount = function(itemCount)
    {
        var iCount = CreateXmlDOM().createAttribute("itemCount");            
        iCount.value = itemCount;
        this.xml.attributes.setNamedItem(iCount);
    }
    this.SetCurrent = function()
    {
        var current = this.xml.attributes.getNamedItem("current");
        
        if (!isMozilla)
            current.text = "true";
        else
            current.textContent = "true";
    }
    this.ClearCurrent = function()
    {
        var current = this.xml.attributes.getNamedItem("current");
        
        if (!isMozilla)
            current.text = "false";
        else
            current.textContent = "false";
    }
    this.Mark = function()
    {        
        if (!isMozilla)
            this.xml.attributes.getNamedItem("marked").text = "true";        
        else
            this.xml.attributes.getNamedItem("marked").textContent = "true";        
    }
    this.UnMark = function()
    {    
        if (!isMozilla)
            this.xml.attributes.getNamedItem("marked").text = "false";        
        else
            this.xml.attributes.getNamedItem("marked").textContent = "false";        
    }
}